package com.training1.core.web.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.training1.core.common.controller.BaseController;
import com.training1.core.common.dao.Page;
import com.training1.core.common.entity.RespJson;
import com.training1.core.common.entity.UserEntity;
import com.training1.core.common.global.signIn.LoginInfoUtil;
import com.training1.core.common.utils.StringUtils;
import com.training1.core.entity.MemberEntity;
import com.training1.core.modules.service.DelegatedAuthorityService;
import com.training1.core.modules.service.LoginService;
import com.training1.core.modules.service.MemberAccountService;
import com.training1.core.modules.service.MemberService;
import com.training1.core.modules.service.SystemUserService;
import com.training1.core.modules.service.UserTestService;
import com.training1.util.SequenceNumberUtil;

@Controller
@RequestMapping("delegatedauthority")
public class DelegatedAuthorityController extends BaseController{
	
	@Resource
	private DelegatedAuthorityService daService;
	
	
	@RequestMapping("authoritysetting")
	public String accountstatement(){
		return "huangyan/delegatedauthoritySetting";
	}
	
	/**
	 * 根据手机号查询会员基本信息
	 * @param f
	 * @return
	 */
	@RequestMapping(value="memberinfo",method=RequestMethod.GET)
	@ResponseBody
	public MemberEntity findMemberInfo(String cell){
		MemberEntity mem = daService.findMemberInfo(cell);
		return mem;
	}
	/**
	 * 判断是否第一次购买
	 * @param f
	 * @return
	 */
	@RequestMapping(value="isfirst",method=RequestMethod.GET)
	@ResponseBody
	public String isFirstBuy(String cell){
		String str = daService.isFirstBuy(cell);
		return str;
	}
	/**
	 * 判断是代理商
	 * @param f
	 * @return
	 */
	@RequestMapping(value="isagents",method=RequestMethod.GET)
	@ResponseBody
	public MemberEntity isAgents(String cell){
		MemberEntity mem = daService.isAgents(cell);
		return mem;
	}
	/**
	 * 插入一条点位订单
	 * @param f
	 * @return
	 */
	@RequestMapping(value="nodeorder",method=RequestMethod.GET)
	@ResponseBody
	public int insertTONodeOrder(String cellphone,String recommenderCellphone,String nodeCount,String cash,String branchId){
		Map<String,Object> map1 = new HashMap<>();
		Map<String,Object> map2 = new HashMap<>();
		int s = 0;
//		订单编号
		String orderNum = SequenceNumberUtil.generateOrderNumber();
		map1.put("orderNumber", orderNum);
		map1.put("cellPhone", cellphone);
		List<Integer> list = daService.getStatus(recommenderCellphone);
		map1.put("nodeSort", 1);
		map1.put("nodeCount", nodeCount);
//		创建申请时间
		Date date = new Date();
		map1.put("cash", cash);
		map1.put("applyTime",date);
		map1.put("paymentType", 1);
		map1.put("status", 1);
		map1.put("createTime", date);
		map1.put("createdBy", cellphone);
		daService.insertTONodeOrder(map1);
		s++;
		
		map2.put("orderNumber", orderNum);
		map2.put("cellPhone", cellphone);
		map2.put("recommenderCellphone", recommenderCellphone);
		map2.put("applyTime", date);
//		推荐点位数
		map2.put("recommenderCount", daService.getRecommendCount(cellphone));
		map2.put("userGiftCard", 0);
		map2.put("userGiftCard", 0);
		
//		判断子点还是母点
		String str = daService.isExist(cellphone);
		if(str != null && str != ""){
			map2.put("nodeType", 2);
			
		}else{
			map2.put("nodeType", 1);
		}
		map2.put("status", 1);
		map2.put("createTime", date);
		map2.put("createdBy", cellphone);
		
		for (int i = 0; i < Integer.parseInt(nodeCount); i++) {
			String nodeNum = SequenceNumberUtil.generateNodeNumber(branchId);
			map2.put("nodeNumber", nodeNum);
			daService.insertTONodeApply(map2);
			s++;
		}
		
		return s;
	}
}
